﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerSchoolDao : ISchoolDao
    {
        public SqlServerSchoolDao()
        {
            SQLUtility.TableName = "School";
        }

        //Trả về đối tượng báo ăn
        private School MakeSchool(DataRow row)
        {
            int _id = int.Parse(row["SchoolID"].ToString());
            string _name = row["SchoolName"].ToString();
            string _schoolNumber = row["SchoolNumber"].ToString();
            string _xa = row["Xa"].ToString();
            string _huyen = row["Huyen"].ToString();
            string _tinh = row["Tinh"].ToString();


         return new School(_id, _name, _schoolNumber, _xa, _huyen, _tinh);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<School> MakeSchools(DataTable dt)  
        {
            IList<School> list = new List<School>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakeSchool(row));
            }
            return list;
        }
        private DbParameter[] Take(School School)
        {
            DbParameter[] parameters = new DbParameter[6];
            parameters[0] = new SqlParameter("@SchoolID", School.Id);
            parameters[1] = new SqlParameter("@SchoolName", School.SchoolNumber);
            parameters[2] = new SqlParameter("@SchoolNumber", School.SchoolNumber);

            parameters[3] = new SqlParameter("@Xa", School.Xa);
            parameters[4] = new SqlParameter("@Huyen", School.Huyen);
            parameters[5] = new SqlParameter("@Tinh", School.Tinh);

            return parameters;
        }

        public IList<Business_Objects.School> GetSchool()
        {
            SQLUtility.TableName = "School";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakeSchools(dt);
        }

        public Business_Objects.School GetSchoolByID(string  SchoolID)
        {
            SQLUtility.TableName = "School";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@SchoolID", SchoolID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakeSchool(row);
            }
        }

        public void InsertSchool(Business_Objects.School School)
        {
            SQLUtility.TableName = "School";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(School), true);
        }

        public int UpdateSchool(Business_Objects.School School)
        {
            SQLUtility.TableName = "School";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(School)); }
            catch { return -1; }
        }

        public int DeleteSchool(Business_Objects.School School)
        {
            SQLUtility.TableName = "School";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(School)); }
            catch { return -1; }
        }
    }
}
